package com.yisu.expressway.ui.recyclerview;

import android.content.Context;
import android.support.annotation.Nullable;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

/**
 * extension of RecyclerView.Adapter to management data.
 *
 * Created by will on 15/9/2.
 */
public abstract class SampleRecyclerViewAdapter<T> extends BaseRvAdapter<T, BaseRvAdapter.SampleViewHolder> {

    private ArrayList<T> mList;
    public Context mContext;
    public OnItemClickLitener mOnItemClickLitener;


    public SampleRecyclerViewAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public int getItemCount() {
        if (mList != null)
            return mList.size();
        else
            return 0;
    }

    public void setData(ArrayList<T> data) {
        this.mList = data;
        notifyDataSetChanged();
    }



    public List<T> convertToList(){
        List<T> list = new ArrayList<>(mList);
        return list;
    }

    public ArrayList<T> getData() {
        return mList;
    }

    public void setData(T[] list) {
        ArrayList<T> arrayList = new ArrayList<>(list.length);
        for (T t : list) {
            arrayList.add(t);
        }
        setData(arrayList);
    }


    public void addData(int position, T item) {
        if(mList == null || mList.size() == 0){
            mList = new ArrayList();
            mList.add(item);
            notifyMyItemInserted(0);
        }else if(position < mList.size()){
            mList.add(position, item);
            notifyMyItemInserted(position);
        }
    }

    public void removeData(int position) {
        if (mList != null && position < mList.size()) {
            mList.remove(position);
            notifyMyItemRemoved(position);
        }
    }

    @Nullable
    public T getItem(int position) {
        if (mList != null && position < mList.size()) {
            return mList.get(position);
        }
        return null;
    }

    protected void notifyMyItemInserted(int position){
        notifyItemInserted(position);
    }

    protected void notifyMyItemRemoved(int position){
        notifyItemRemoved(position);
    }

    protected void notifyMyItemChanged(int position){
        notifyItemChanged(position);
    }

    public interface OnItemClickLitener {
        void onItemClick(View view, int position);
    }

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }



//    public static class BaseViewHolder extends ToroVideoView{
//        private SparseArray<View> views = new SparseArray<View>();
//
//        public SampleViewHolder(View itemView) {
//            super(itemView);
//        }
//
//        @SuppressWarnings("unchecked")
//        public <T extends View> T getView(int resId) {
//            View v = views.get(resId);
//            if (null == v) {
//                v = itemView.findViewById(resId);
//                views.put(resId, v);
//            }
//            return (T) v;
//        }
//    }
//
//
//    public static class SampleViewHolder extends ToroViewHolder {
//        private SparseArray<View> views = new SparseArray<View>();
//
//        public SampleViewHolder(View itemView) {
//            super(itemView);
//        }
//
//        @SuppressWarnings("unchecked")
//        public <T extends View> T getView(int resId) {
//            View v = views.get(resId);
//            if (null == v) {
//                v = itemView.findViewById(resId);
//                views.put(resId, v);
//            }
//            return (T) v;
//        }
//    }

}
